package de.sendorian.app.forumArchive.dundjinni;

import static de.sendorian.app.forumArchive.matcher.ForumMatcher.matchesDateInterval;
import static de.sendorian.app.forumArchive.matcher.ForumMatcher.matchesDateIntervalUntilNow;
import static de.sendorian.app.forumArchive.renamer.Renamers.appendUserShortname;
import static de.sendorian.app.forumArchive.renamer.Renamers.both;
import static de.sendorian.app.forumArchive.renamer.Renamers.capitalizeFirst;
import static de.sendorian.app.forumArchive.renamer.Renamers.removeForbiddenChars;
import static de.sendorian.app.forumArchive.renamer.Renamers.removeHash;
import static org.hamcrest.Matchers.anything;

import java.text.SimpleDateFormat;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.hamcrest.Matcher;
import org.springframework.stereotype.Component;

import de.sendorian.app.forumArchive.Config;
import de.sendorian.app.forumArchive.domain.Forum;
import de.sendorian.app.forumArchive.domain.Post;
import de.sendorian.app.forumArchive.domain.Topic;
import de.sendorian.app.forumArchive.finder.Finder;
import de.sendorian.app.forumArchive.renamer.FileRenamer;


@Component
public class DundjinniConfigStrategy extends Config {

	private static final String BASE_URL = "http://www.dundjinni.com/forums/";

	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy h:mma");

	private Pattern DATE_PATTERN = Pattern.compile("(\\d{2}/\\d{2}/\\d{4}).*at.*(\\d{1,2}:\\d{2}.m)");

	Finder<String, Forum> forumFinder;
	Finder<Topic, Post> postFinder;
	Finder<Forum, Topic> topicFinder;


	public DundjinniConfigStrategy() {
		config = this;
	}


	public String getBaseUrl() {
		return BASE_URL;
	}


	public SimpleDateFormat getDateFormat() {
		return DATE_FORMAT;
	}


	public Pattern getDatePattern() {
		return DATE_PATTERN;
	}


	@Resource(name = "dundjinniForumFinder")
	public void setForumFinder(Finder<String, Forum> forumFinder) {
		this.forumFinder = forumFinder;
	}


	public Finder<String, Forum> getForumFinder() {
		return forumFinder;
	}


	@Resource(name = "dundjinniPostFinder")
	public void setPostFinder(Finder<Topic, Post> postFinder) {
		this.postFinder = postFinder;
	}


	public Finder<Topic, Post> getPostFinder() {
		return postFinder;
	}


	@Resource(name = "dundjinniTopicFinder")
	public void setTopicFinder(Finder<Forum, Topic> topicFinder) {
		this.topicFinder = topicFinder;
	}


	public Finder<Forum, Topic> getTopicFinder() {
		return topicFinder.withFilter(getTopicMatcher());
	}


	public Matcher<Post> getPostMatcher() {
		if (getStartDate() != null && getEndDate() != null) {
			return matchesDateInterval(getStartDate(), getEndDate());
		}
		return anything();
	}


	public Matcher<Topic> getTopicMatcher() {
		if (getStartDate() != null) {
			return matchesDateIntervalUntilNow(getStartDate());
		}
		return anything();
	}


	public FileRenamer getDefaultRenamer() {
		return both(removeHash()).and(capitalizeFirst()).and(appendUserShortname()).and(removeForbiddenChars());
	}
}
